<?php


abstract class BasePeople extends BaseObject  implements Persistent {


  const PEER = 'PeoplePeer';

	
	protected static $peer;

	
	protected $id;

	
	protected $name;

	
	protected $apppassword;

	
	protected $card;

	
	protected $role;

	
	protected $visible;

	
	protected $image;

	
	protected $aRoles;

	
	protected $collTicketss;

	
	private $lastTicketsCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		parent::__construct();
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getName()
	{
		return $this->name;
	}

	
	public function getApppassword()
	{
		return $this->apppassword;
	}

	
	public function getCard()
	{
		return $this->card;
	}

	
	public function getRole()
	{
		return $this->role;
	}

	
	public function getVisible()
	{
		return $this->visible;
	}

	
	public function getImage()
	{
		return $this->image;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = PeoplePeer::ID;
		}

		return $this;
	} 
	
	public function setName($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->name !== $v) {
			$this->name = $v;
			$this->modifiedColumns[] = PeoplePeer::NAME;
		}

		return $this;
	} 
	
	public function setApppassword($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->apppassword !== $v) {
			$this->apppassword = $v;
			$this->modifiedColumns[] = PeoplePeer::APPPASSWORD;
		}

		return $this;
	} 
	
	public function setCard($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->card !== $v) {
			$this->card = $v;
			$this->modifiedColumns[] = PeoplePeer::CARD;
		}

		return $this;
	} 
	
	public function setRole($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->role !== $v) {
			$this->role = $v;
			$this->modifiedColumns[] = PeoplePeer::ROLE;
		}

		if ($this->aRoles !== null && $this->aRoles->getId() !== $v) {
			$this->aRoles = null;
		}

		return $this;
	} 
	
	public function setVisible($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->visible !== $v) {
			$this->visible = $v;
			$this->modifiedColumns[] = PeoplePeer::VISIBLE;
		}

		return $this;
	} 
	
	public function setImage($v)
	{
								if (!is_resource($v)) {
			$this->image = fopen('php://memory', 'r+');
			fwrite($this->image, $v);
			rewind($this->image);
		} else { 			$this->image = $v;
		}
		$this->modifiedColumns[] = PeoplePeer::IMAGE;

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (string) $row[$startcol + 0] : null;
			$this->name = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
			$this->apppassword = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->card = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
			$this->role = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
			$this->visible = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
			if ($row[$startcol + 6] !== null) {
				$this->image = fopen('php://memory', 'r+');
				fwrite($this->image, $row[$startcol + 6]);
				rewind($this->image);
			} else {
				$this->image = null;
			}
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 7; 
		} catch (Exception $e) {
			throw new PropelException("Error populating People object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aRoles !== null && $this->role !== $this->aRoles->getId()) {
			$this->aRoles = null;
		}
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(PeoplePeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = PeoplePeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aRoles = null;
			$this->collTicketss = null;
			$this->lastTicketsCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(PeoplePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			PeoplePeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(PeoplePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			$con->commit();
			PeoplePeer::addInstanceToPool($this);
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aRoles !== null) {
				if ($this->aRoles->isModified() || $this->aRoles->isNew()) {
					$affectedRows += $this->aRoles->save($con);
				}
				$this->setRoles($this->aRoles);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = PeoplePeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setNew(false);
				} else {
					$affectedRows += PeoplePeer::doUpdate($this, $con);
				}

								if ($this->image !== null && is_resource($this->image)) {
					rewind($this->image);
				}

				$this->resetModified(); 			}

			if ($this->collTicketss !== null) {
				foreach ($this->collTicketss as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aRoles !== null) {
				if (!$this->aRoles->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aRoles->getValidationFailures());
				}
			}


			if (($retval = PeoplePeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collTicketss !== null) {
					foreach ($this->collTicketss as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = PeoplePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		$field = $this->getByPosition($pos);
		return $field;
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getName();
				break;
			case 2:
				return $this->getApppassword();
				break;
			case 3:
				return $this->getCard();
				break;
			case 4:
				return $this->getRole();
				break;
			case 5:
				return $this->getVisible();
				break;
			case 6:
				return $this->getImage();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = PeoplePeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getName(),
			$keys[2] => $this->getApppassword(),
			$keys[3] => $this->getCard(),
			$keys[4] => $this->getRole(),
			$keys[5] => $this->getVisible(),
			$keys[6] => $this->getImage(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = PeoplePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setName($value);
				break;
			case 2:
				$this->setApppassword($value);
				break;
			case 3:
				$this->setCard($value);
				break;
			case 4:
				$this->setRole($value);
				break;
			case 5:
				$this->setVisible($value);
				break;
			case 6:
				$this->setImage($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = PeoplePeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setName($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setApppassword($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setCard($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setRole($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setVisible($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setImage($arr[$keys[6]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(PeoplePeer::DATABASE_NAME);

		if ($this->isColumnModified(PeoplePeer::ID)) $criteria->add(PeoplePeer::ID, $this->id);
		if ($this->isColumnModified(PeoplePeer::NAME)) $criteria->add(PeoplePeer::NAME, $this->name);
		if ($this->isColumnModified(PeoplePeer::APPPASSWORD)) $criteria->add(PeoplePeer::APPPASSWORD, $this->apppassword);
		if ($this->isColumnModified(PeoplePeer::CARD)) $criteria->add(PeoplePeer::CARD, $this->card);
		if ($this->isColumnModified(PeoplePeer::ROLE)) $criteria->add(PeoplePeer::ROLE, $this->role);
		if ($this->isColumnModified(PeoplePeer::VISIBLE)) $criteria->add(PeoplePeer::VISIBLE, $this->visible);
		if ($this->isColumnModified(PeoplePeer::IMAGE)) $criteria->add(PeoplePeer::IMAGE, $this->image);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(PeoplePeer::DATABASE_NAME);

		$criteria->add(PeoplePeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setId($this->id);

		$copyObj->setName($this->name);

		$copyObj->setApppassword($this->apppassword);

		$copyObj->setCard($this->card);

		$copyObj->setRole($this->role);

		$copyObj->setVisible($this->visible);

		$copyObj->setImage($this->image);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getTicketss() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addTickets($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new PeoplePeer();
		}
		return self::$peer;
	}

	
	public function setRoles(Roles $v = null)
	{
		if ($v === null) {
			$this->setRole(NULL);
		} else {
			$this->setRole($v->getId());
		}

		$this->aRoles = $v;

						if ($v !== null) {
			$v->addPeople($this);
		}

		return $this;
	}


	
	public function getRoles(PropelPDO $con = null)
	{
		if ($this->aRoles === null && (($this->role !== "" && $this->role !== null))) {
			$c = new Criteria(RolesPeer::DATABASE_NAME);
			$c->add(RolesPeer::ID, $this->role);
			$this->aRoles = RolesPeer::doSelectOne($c, $con);
			
		}
		return $this->aRoles;
	}

	
	public function clearTicketss()
	{
		$this->collTicketss = null; 	}

	
	public function initTicketss()
	{
		$this->collTicketss = array();
	}

	
	public function getTicketss($criteria = null, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(PeoplePeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTicketss === null) {
			if ($this->isNew()) {
			   $this->collTicketss = array();
			} else {

				$criteria->add(TicketsPeer::PERSON, $this->id);

				TicketsPeer::addSelectColumns($criteria);
				$this->collTicketss = TicketsPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(TicketsPeer::PERSON, $this->id);

				TicketsPeer::addSelectColumns($criteria);
				if (!isset($this->lastTicketsCriteria) || !$this->lastTicketsCriteria->equals($criteria)) {
					$this->collTicketss = TicketsPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastTicketsCriteria = $criteria;
		return $this->collTicketss;
	}

	
	public function countTicketss(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(PeoplePeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collTicketss === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(TicketsPeer::PERSON, $this->id);

				$count = TicketsPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(TicketsPeer::PERSON, $this->id);

				if (!isset($this->lastTicketsCriteria) || !$this->lastTicketsCriteria->equals($criteria)) {
					$count = TicketsPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collTicketss);
				}
			} else {
				$count = count($this->collTicketss);
			}
		}
		$this->lastTicketsCriteria = $criteria;
		return $count;
	}

	
	public function addTickets(Tickets $l)
	{
		if ($this->collTicketss === null) {
			$this->initTicketss();
		}
		if (!in_array($l, $this->collTicketss, true)) { 			array_push($this->collTicketss, $l);
			$l->setPeople($this);
		}
	}


	
	public function getTicketssJoinReceipts($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
	{
		if ($criteria === null) {
			$criteria = new Criteria(PeoplePeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTicketss === null) {
			if ($this->isNew()) {
				$this->collTicketss = array();
			} else {

				$criteria->add(TicketsPeer::PERSON, $this->id);

				$this->collTicketss = TicketsPeer::doSelectJoinReceipts($criteria, $con, $join_behavior);
			}
		} else {
									
			$criteria->add(TicketsPeer::PERSON, $this->id);

			if (!isset($this->lastTicketsCriteria) || !$this->lastTicketsCriteria->equals($criteria)) {
				$this->collTicketss = TicketsPeer::doSelectJoinReceipts($criteria, $con, $join_behavior);
			}
		}
		$this->lastTicketsCriteria = $criteria;

		return $this->collTicketss;
	}


	
	public function getTicketssJoinCustomers($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
	{
		if ($criteria === null) {
			$criteria = new Criteria(PeoplePeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTicketss === null) {
			if ($this->isNew()) {
				$this->collTicketss = array();
			} else {

				$criteria->add(TicketsPeer::PERSON, $this->id);

				$this->collTicketss = TicketsPeer::doSelectJoinCustomers($criteria, $con, $join_behavior);
			}
		} else {
									
			$criteria->add(TicketsPeer::PERSON, $this->id);

			if (!isset($this->lastTicketsCriteria) || !$this->lastTicketsCriteria->equals($criteria)) {
				$this->collTicketss = TicketsPeer::doSelectJoinCustomers($criteria, $con, $join_behavior);
			}
		}
		$this->lastTicketsCriteria = $criteria;

		return $this->collTicketss;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			if ($this->collTicketss) {
				foreach ((array) $this->collTicketss as $o) {
					$o->clearAllReferences($deep);
				}
			}
		} 
		$this->collTicketss = null;
			$this->aRoles = null;
	}

} 